#.rst:
# botan-config.cmake
# -----------
#
# Find the botan library.
#
# This CMake configuration file, installed as part of the Botan build,
# provides support for find_package(Botan).
#
# Required version(s) can be passed as usual:
# find_package(Botan 3.3.0 REQUIRED)
#
# COMPONENTS and OPTIONAL_COMPONENTS can be used to specify Botan
# modules that must or should be enabled in the Botan build:
# find_package(Botan 3.3.0 COMPONENTS rsa ecdsa)
#
# IMPORTED Targets
# ^^^^^^^^^^^^^^^^
#
# This module defines :prop_tgt:`IMPORTED` targets:
#
# ``Botan::Botan``
#   The botan shared library, if found.
# ``Botan::Botan-static``
#   The botan static library, if found.
#
# Result variables
# ^^^^^^^^^^^^^^^^
#
# This module defines the following variables:
#
# ::
#
#   Botan_FOUND          - true if the headers and library were found
#   Botan_VERSION        - library version that was found, if any
#

set(_Botan_supported_components
%{for mod_list}
%{i}
%{endfor}
)

unset(${CMAKE_FIND_PACKAGE_NAME}_FOUND)
unset(_Botan_missing_required_modules)

foreach(_comp IN LISTS ${CMAKE_FIND_PACKAGE_NAME}_FIND_COMPONENTS)
  if (NOT _comp IN_LIST _Botan_supported_components)
    set(${CMAKE_FIND_PACKAGE_NAME}_${_comp}_FOUND False)
    if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED_${_comp})
      list(APPEND _Botan_missing_required_modules ${_comp})
    endif()
  else()
    set(${CMAKE_FIND_PACKAGE_NAME}_${_comp}_FOUND True)
  endif()
endforeach()

if(_Botan_missing_required_modules)
  set(${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
  list(JOIN _Botan_missing_required_modules ", " _missing_modules)
  set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "Unsupported module(s): ${_missing_modules}")
endif()

if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_FOUND AND NOT ${${CMAKE_FIND_PACKAGE_NAME}_FOUND})
  return()
endif()

# botan-config.cmake lives in "${_Botan_PREFIX}/lib/cmake/Botan-X": traverse up to $_Botan_PREFIX
set(_Botan_PREFIX "${CMAKE_CURRENT_LIST_DIR}")
get_filename_component(_Botan_PREFIX "${_Botan_PREFIX}" DIRECTORY)
get_filename_component(_Botan_PREFIX "${_Botan_PREFIX}" DIRECTORY)
get_filename_component(_Botan_PREFIX "${_Botan_PREFIX}" DIRECTORY)

%{if build_static_lib}
if(NOT TARGET Botan::Botan-static)
  add_library(Botan::Botan-static STATIC IMPORTED)
  set_target_properties(Botan::Botan-static
    PROPERTIES
      IMPORTED_LOCATION                 "${_Botan_PREFIX}/lib/%{static_lib_name}"
      INTERFACE_INCLUDE_DIRECTORIES     "${_Botan_PREFIX}/include/botan-%{version_major}"
      IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
      INTERFACE_LINK_OPTIONS            "SHELL:%{cxx_abi_flags}")
endif()
%{endif}

%{if implib_name}
set(_Botan_implib     "${_Botan_PREFIX}/lib/%{implib_name}")
set(_Botan_shared_lib "${_Botan_PREFIX}/bin/%{shared_lib_name}")
%{endif}
%{unless implib_name}
set(_Botan_implib "")
%{endif}

%{if build_shared_lib}
if(NOT TARGET Botan::Botan)
  if(NOT DEFINED _Botan_shared_lib)
    set(_Botan_shared_lib "${_Botan_PREFIX}/lib/%{shared_lib_name}")
  endif()

  add_library(Botan::Botan SHARED IMPORTED)
  set_target_properties(Botan::Botan
    PROPERTIES
      IMPORTED_LOCATION             "${_Botan_shared_lib}"
      IMPORTED_IMPLIB               "${_Botan_implib}"
      INTERFACE_INCLUDE_DIRECTORIES "${_Botan_PREFIX}/include/botan-%{version_major}"
      INTERFACE_LINK_OPTIONS        "SHELL:%{cxx_abi_flags}")
  set_property(TARGET Botan::Botan APPEND PROPERTY IMPORTED_CONFIGURATIONS NOCONFIG)
  set_target_properties(Botan::Botan
    PROPERTIES
      IMPORTED_LOCATION_NOCONFIG "${_Botan_PREFIX}/lib/%{shared_lib_name}"
      IMPORTED_SONAME_NOCONFIG   "%{shared_lib_name}"
      IMPORTED_IMPLIB_NOCONFIG   "${_Botan_implib}")
endif()
%{endif}

set(${CMAKE_FIND_PACKAGE_NAME}_FOUND True)
